﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CamHelper.Engine;
using SystemWrapper.IO;

namespace CamHelper.Interpreter
{
    public class ParameterManager : IParameterManager
    {
        public IParameterCollection AllParameter { get; private set; }
        public IParameterCollection ZmiOpUser { get; private set; }
        public IParameterCollection ZmiOpCam { get; private set; }
        public IParameterCollection ZmiOpCamTurn { get; private set; }
        private IConfig _config;
        private IFileWrap _fileWrap;

        public ParameterManager(IConfig config, IFileWrap fileWrap)
        {
            _config = config;
            _fileWrap = fileWrap;
            Reload();
        }

        #region Reload

        public void Reload()
        {
            var pathZmiOpUser = Path.Combine(_config.TopConfigDirectory, "ZmiOpUser.par");
            if (_fileWrap.Exists(pathZmiOpUser))
            {
                ZmiOpUser = new ParameterFile(pathZmiOpUser, _fileWrap).Parameters;
            }

            var pathZmiOpCam = Path.Combine(_config.TopHomeDirectory, "bin\\ZmiOpCam.par");
            if (_fileWrap.Exists(pathZmiOpCam))
            {
                ZmiOpCam = new ParameterFile(pathZmiOpCam, _fileWrap).Parameters;
            }

            var pathZmiOpCamTurn = Path.Combine(_config.TopHomeDirectory, "bin\\ZmiOpCamTurn.par");
            if (_fileWrap.Exists(pathZmiOpCamTurn))
            {
                ZmiOpCamTurn = new ParameterFile(pathZmiOpCamTurn, _fileWrap).Parameters;
            }

            AllParameter = new ParameterCollection();
            AllParameter.AddRange(ZmiOpCam);
            AllParameter.AddRange(ZmiOpCamTurn);
            AllParameter.AddRange(ZmiOpUser);
        }

        #endregion

        #region Search

        public IParameterCollection Search(string categorie, string word, bool onlyZmiOpUser)
        {
            var source = (onlyZmiOpUser ? ZmiOpUser : AllParameter);
            IEnumerable<KeyValuePair<String, IParameter>> result = null;

            if (categorie.Length > 0 && word.Length > 0)
            {
                result = from p in source
                         where p.Value.Category == categorie && p.Key.IndexOf(word) > 0
                         select p;
            }
            else if (categorie.Length > 0)
            {
                result = from p in source
                         where p.Value.Category == categorie
                         select p;
            }
            else if (word.Length > 0)
            {
                result = from p in source
                         where p.Key.IndexOf(word) > 0
                         select p;
            }
            else
            {
                return source;
            }

            var pc = new ParameterCollection();
            foreach (var p in result)
            {
                pc.Add(p.Value);
            }

            return pc;
        }

        #endregion

    }
}
